package com.example.administrator.sondpooldemo;

import java.util.HashMap;
import java.util.Map;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.media.AudioManager;
import android.media.SoundPool;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

public class MainActivity extends Activity implements OnClickListener{
	private Button btn1;
	private Button btn2;
	private Button btn3;
	int max;
	int current;
	private SoundPool sp; //������
	private Map<Integer, Integer> mapSRC;
	AudioManager am;
	int media;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		am = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
// 调高音量
		EventBus.getDefault().register(this);
		//current = am.getStreamVolume(AudioManager.STREAM_SYSTEM);
		media=am.getStreamVolume(AudioManager.STREAM_MUSIC);
		Log.e("service", "设置前  媒体音量"+media);
        am.setStreamVolume(AudioManager.STREAM_MUSIC,am.getStreamMaxVolume(AudioManager.STREAM_MUSIC),AudioManager.FLAG_SHOW_UI);
		current=am.getStreamVolume(AudioManager.STREAM_MUSIC);
		Log.e("service", "设置后   媒体音量值：" + current);
//		initView();
//		initSoundPool();
		startActivity(new Intent(this,Two.class));
	}

	private void initView(){
		btn1 = (Button) findViewById(R.id.button1);
		btn2 = (Button) findViewById(R.id.button2);
		btn3 = (Button) findViewById(R.id.button3);
		
		btn1.setOnClickListener(this);
		btn2.setOnClickListener(this);
		btn3.setOnClickListener(this);

	}
	
	//��ʼ��������
	private void initSoundPool(){
		sp = new SoundPool(1, AudioManager.STREAM_SYSTEM, 5);
		mapSRC = new HashMap<Integer, Integer>();
		mapSRC.put(1, sp.load(this, R.raw.avchat_no_response, 0));
		mapSRC.put(2, sp.load(this, R.raw.avchat_peer_busy, 0));
		mapSRC.put(3, sp.load(this, R.raw.avchat_peer_reject, 0));
	}
	
	/**
	 * ���������ص�����
	 * @param sound
	 * @param number
	 */
	private void play(int sound, int number){
		sp.play(mapSRC.get(sound),//���ŵ�������Դ
				1.0f,//����������ΧΪ0--1.0
				1.0f,//����������ΧΪ0--1.0
				0, //���ȼ���0Ϊ������ȼ�
				number,//ѭ������,0Ϊ��ѭ��
				0);//�������ʣ�0Ϊ��������
	}
	@Subscribe
	public void onEvent(String  event) {
    /* Do something */
    Log.e("iiiiii","收到");
	}
	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		EventBus.getDefault().register(this);
		am.setStreamVolume(AudioManager.STREAM_MUSIC,media,AudioManager.FLAG_SHOW_UI);
		media=am.getStreamVolume(AudioManager.STREAM_MUSIC);
		Log.e("service", "destroy 系统音量值"+media);
		if(sp != null){
			sp.release();
		}
		super.onDestroy();
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.button1:
			play(1, 1);
			break;
		case R.id.button2:
			play(2, 1);
			break;
		case R.id.button3:
			play(3, 1);
			break;

		default:
			break;
		}
		
	}

}
